<?php
require_once(dirname(__FILE__).'/../../../../wp-load.php');


function db_result($option_name){
	global $wpdb;
//	$strSQL	= "SELECT option_name from wp27_options where option_name like 'wap%'";
//	$comments	= $wpdb->get_results($strSQL);
	$strSQL	=	"SELECT option_value FROM $wpdb->options WHERE option_name = '$option_name'";
	$result	=	$wpdb->get_row($wpdb->prepare($strSQL));

	return $result;
}

//ȡ��options���е�ֵ
function db_options($option_name){
	global $wpdb;
	$strSQL	=	"SELECT option_value FROM $wpdb->options WHERE option_name = '$option_name'";
	$result	=	$wpdb->get_var($wpdb->prepare($strSQL));
	//return	$result[0]->option_value;
	return $result;
}

function db_var($param){
	global $wpdb;
	if($param == 'post_num'){
		$strSQL	=	"SELECT count(*) AS page_num FROM $wpdb->posts WHERE post_type='post' AND post_status='publish'";
		$result	=	$wpdb->get_var($wpdb->prepare($strSQL));
		//return	$result[0]->option_value;
	}
	return $result;
}

function db_postlist($term, $pagenum, $range) {
	global $wpdb;
//	var $termval	= '';
	$limit	= array('start'=>($pagenum-1)*$range, 'end' => $pagenum*$range);
	$strSQL	=	"SELECT ID, post_title AS title, post_date AS date, post_author AS author FROM $wpdb->posts WHERE 1=1 AND post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT ".$limit['start'].", ".$range;
	$result	=	$wpdb->get_results($wpdb->prepare($strSQL));
	return $result;
}

function db_post($post_id) {
	global $wpdb;
	$strSQL	=	"SELECT ID, post_author AS author, post_date AS date, post_content AS content, post_title AS title, post_category AS cat, comment_count AS comment_count FROM $wpdb->posts WHERE ID=$post_id";
	$result	=	$wpdb->get_row($wpdb->prepare($strSQL));
	$result->content	=	preg_replace('/\r\n/i','<br />',$result->content);
	return $result;
}

function db_cat($post_id){
	global $wpdb;
	$strSQL	=	"SELECT T.term_id AS id, T.name AS name FROM $wpdb->term_taxonomy AS TT,$wpdb->terms AS T,$wpdb->term_relationships AS TR WHERE TT.taxonomy='category' AND TT.term_taxonomy_id=TR.term_taxonomy_id AND TT.term_id=T.term_id AND TR.object_id=$post_id";
	$result	=	$wpdb->get_results($wpdb->prepare($strSQL));
	return $result;
}

function db_author($author){
	global $wpdb;
	$strSQL	=	"SELECT display_name FROM $wpdb->users WHERE ID=$author";
	$result	=	$wpdb->get_var($wpdb->prepare($strSQL));
	return $result;
}

function db_usermeta($userid, $meta_key = null){
	global $wpdb;
	if(empty($meta_key)){
		$strSQL	=	"SELECT meta_value FROM $wpdb->usermeta WHERE user_id=$userid";
	}
	else{
		$strSQL	=	"SELECT meta_value FROM $wpdb->usermeta WHERE user_id=$userid and meta_key='$meta_key'";
	}
	$result	=	$wpdb->get_row($wpdb->prepare($strSQL));
	return $result;
}

?>